package statistics;

import main.Simulator;
import main.Util;
import model.BackgroundPacket;

import org.jfree.data.xy.XYSeries;

import event.Event;
import event.PacketReceivedByReceiverEvent;
import event.PacketSentByRouterEvent;

public class VazaoMediaStatistics extends Statistics {

	long packets[];
	int n;
	
	public VazaoMediaStatistics(Simulator simulator, String name, String xLabel, String yLabel, int n) {
		super(simulator, name, xLabel, yLabel, n+1);
		this.title = name + "\n" + "Vazão Média";
		this.simulator = simulator;
		this.n = n;
		this.packets = new long[n+1];
		for (int i = 0; i <= n; i++)
			this.packets[i] = 0;
		
		xySeries[n] = new XYSeries("Tráfego de Fundo");
		// Inicializa o gráfico
		for (int sessionId = 0; sessionId <= n; sessionId++) {
			xySeries[sessionId].add(0, 0);
		}
	}

	@Override
	public void collect(Event event) {
		if (event instanceof PacketReceivedByReceiverEvent) {
			int sessionId = ((PacketReceivedByReceiverEvent) event).getPack().getSessionId();
			packets[sessionId]++;
			xySeries[sessionId].add(simulator.getTime(), packets[sessionId]/Util.convertMillisToSeconds(simulator.getTime()));			
		}
		else if(event instanceof PacketSentByRouterEvent) {
			PacketSentByRouterEvent e = (PacketSentByRouterEvent) event;
			if (e.getPack() instanceof BackgroundPacket) {
				packets[n]++;
				xySeries[n].add(simulator.getTime(), packets[n]/Util.convertMillisToSeconds(simulator.getTime()));			
			}
		}
		
	}
	
	public void saveAsFile() {
		saveAsImage(name + "-" + "VazaoMedia");
	}

}
